package com.ybkj.daijia.server.event.listener;

import com.ybkj.daijia.PushData;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiMember;
import com.ybkj.daijia.common.HtmlHelper;
import com.ybkj.daijia.server.event.model.MemberNoticeEvent;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.model.MemberNotice;
import com.ybkj.daijia.server.service.MemberAccountService;
import com.ybkj.daijia.server.service.MemberService;
import java.io.IOException;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class MemberNoticeEventListener implements ApplicationListener<MemberNoticeEvent> {

    private static final Logger logger = LoggerFactory.getLogger(MemberNoticeEventListener.class);

    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberAccountService memberAccountService;

    @Autowired
    private SettingUtils settingUtils;


    @Override
    @Async
    public void onApplicationEvent(MemberNoticeEvent event) {

        logger.debug("admin MemberNoticeEvent event:{}", "");

        MemberNotice notice = (MemberNotice) event.getSource();
        List<Passenger> passengers = event.getPassengers();

        Setting setting = settingUtils.get();

        for (Passenger passenger : passengers) {

            passenger.setCid(memberService.findCid(passenger.getId()));

            if (StringUtils.isNotBlank(passenger.getCid())) {
                try {

                    String text = HtmlHelper.html2Text(notice.getContent());
                    if (text.length() > 600) {
                        text = text.substring(0, 600);
                    }

                    //发送设备推送
                    PushData pushData = new PushData();
                    pushData.setType("NEW_NOTICE");
                    pushData.setTitle("新通知");
                    pushData.setDescription(text);
                    if (StringUtils.isNotBlank(notice.getContentMore())) {
                        String url =
                            setting.getWebUrl() + "/member/api/rest/v3/viewNotice/" + notice
                                .getId();
                        pushData.setLink(url);
                    }
                    pushData.setData(notice.getId());

                    if (null != pushData.getLink()) {
                        GeTuiMember.notice(passenger, pushData, setting);
                    }
                    GeTuiMember.push(passenger, pushData, setting);


                } catch (IOException e) {
                    logger.debug("push error : " + e.getMessage());
                }
            }

        }


    }

}
